<!--
  Copyright 2016-2018 the original author or authors.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- clearfix is needed for smaller screens where gt-config-return-link is floated  -->
<div class="clearfix">
  <a class="gt-config-return-link"
     href="admin/integration-list">
    Return to list
  </a>
</div>
<div class="card">
  <div class="card-header">
    <h2>
      Integrations
      <span class="d-inline-block gt-separator">
        |
      </span>
      <span style="font-size: 24px;">LDAP</span>
    </h2>
  </div>
  <div class="card-body">
    <div ng-include src="'template/gt-loading-overlay.html'"></div>
    <div ng-include src="'template/gt-http-error-overlay.html'"></div>
    <div ng-form
         gt-form-autofocus-on-first-input
         name="formCtrl"
         style="padding-top: 15px;">
      <fieldset class="gt-fieldset">
        <legend class="gt-legend">
          Connection
        </legend>
        <div gt-form-group
             gt-label="Host"
             gt-model="config.host"
             gt-disabled="!layout.adminEdit"
             gt-width="20em">
          <div class="help-block">
            LDAP server hostname or IP address.
          </div>
        </div>
        <div gt-form-group
             gt-label="Port"
             gt-model="config.port"
             gt-number="true"
             gt-pattern="pattern.integer"
             gt-disabled="!layout.adminEdit"
             gt-width="7em">
          <div class="help-block">
            Defaults to port 389 for non-SSL, and 636 for SSL.
          </div>
        </div>
        <div gt-form-group
             gt-type="checkbox"
             gt-label="SSL"
             gt-checkbox-label="Use SSL"
             gt-model="config.ssl"
             gt-disabled="!layout.adminEdit">
        </div>
        <div gt-form-group
             gt-label="Username"
             gt-model="config.username"
             gt-required="config.host"
             gt-disabled="!layout.adminEdit"
             gt-width="20em">
          <div class="help-block">
            Username that Glowroot uses to bind and run LDAP queries.
          </div>
        </div>
        <div class="form-group row">
          <label class="col-xl-3 gt-form-label-xl"
                 for="password">
            Password
          </label>
          <div class="col-xl-9">
            <input type="password"
                   class="form-control"
                   ng-model="password"
                   ng-required="config.host"
                   ng-disabled="!layout.adminEdit"
                   name="password"
                   id="password"
                   ng-change="onPasswordChange()"
                   ng-click="onPasswordClick()"
                   style="max-width: 20em; display: inline-block">
            <div class="gt-invalid-required-feedback">
              This is required.
            </div>
            <div class="help-block">
              Password that Glowroot uses to bind and run LDAP queries.
            </div>
          </div>
        </div>
      </fieldset>
      <fieldset class="gt-fieldset">
        <legend class="gt-legend">
          Structure
        </legend>
        <div gt-form-group
             gt-label="User base DN"
             gt-model="config.userBaseDn"
             gt-required="config.host"
             gt-disabled="!layout.adminEdit"
             gt-width="25em">
          <div class="help-block">
            Base DN for locating users, e.g. <em>ou=Users,dc=example,dc=com</em>
          </div>
        </div>
        <div gt-form-group
             gt-label="User search filter"
             gt-model="config.userSearchFilter"
             gt-required="config.host"
             gt-disabled="!layout.adminEdit"
             gt-width="25em">
          <div class="help-block">
            This search filter is used to find the user based on the username they enter during login.
            The placeholder {0} in the filter will be populated with the username.
            For active directory this filter is likely <em>sAMAccountName={0}</em>
          </div>
        </div>
        <div gt-form-group
             gt-label="Group base DN"
             gt-model="config.groupBaseDn"
             gt-required="config.host"
             gt-disabled="!layout.adminEdit"
             gt-width="25em">
          <div class="help-block">
            Base DN for locating groups, e.g. <em>ou=Groups,dc=example,dc=com</em>
          </div>
        </div>
        <div gt-form-group
             gt-label="Group search filter"
             gt-model="config.groupSearchFilter"
             gt-required="config.host"
             gt-disabled="!layout.adminEdit"
             gt-width="25em">
          <div class="help-block">
            This search filter is used to find the groups that a user is a member of.
            The placeholder {0} in the filter will be populated with the user DN.
            For active directory this filter is likely <em>member={0}</em>
          </div>
        </div>
      </fieldset>
      <fieldset class="gt-fieldset"
                ng-repeat="roleMappingBlock in page.roleMappingBlocks">
        <legend class="gt-legend">
          Role mapping
        </legend>
        <div gt-form-group
             gt-label="LDAP group DN"
             gt-model="roleMappingBlock.ldapGroupDn"
             gt-required="true"
             gt-disabled="!layout.adminEdit"
             gt-width="40em">
          <div class="help-block">
            Full distinguished name (DN).
          </div>
        </div>
        <div class="form-group row">
          <label class="col-xl-3 gt-form-label-xl"
                 for="role{{$index}}">
            Glowroot roles
          </label>
          <div class="col-xl-9">
            <select multiple
                    class="form-control"
                    ng-model="roleMappingBlock.glowrootRoles"
                    ng-options="glowrootRole as glowrootRole for glowrootRole in allGlowrootRoles"
                    name="role{{$index}}"
                    id="role{{$index}}"
                    style="width: auto; min-width: 200px; border-top-right-radius: 0; border-bottom-right-radius: 0;"
                    required>
            </select>
            <div class="gt-invalid-required-feedback">
              This is required.
            </div>
          </div>
        </div>
        <div class="form-group row"
             ng-if="layout.adminEdit">
          <div class="offset-xl-3 col-xl-9">
            <button class="btn btn-danger float-right"
                    ng-click="removeRoleMappingBlock(roleMappingBlock)">
              Remove this role mapping
            </button>
          </div>
        </div>
      </fieldset>
      <fieldset class="gt-fieldset"
                ng-if="layout.adminEdit">
        <legend class="gt-legend">
          Add {{page.roleMappingBlocks.length ? 'another' : 'a'}} role mapping
        </legend>
        <button class="btn btn-secondary"
                ng-click="addRoleMappingBlock()"
                style="margin-bottom: 23px; margin-left: 14px;">
          Add
        </button>
      </fieldset>
      <div class="form-group row gt-form-buttons-below-fieldset"
           ng-if="layout.adminEdit">
        <div class="offset-xl-3 col-xl-9">
          <div gt-button
               gt-label="Save changes"
               gt-click="save(deferred)"
               gt-validate-form="formCtrl">
          </div>
        </div>
      </div>
    </div>
    <div ng-form
         class="pt-3"
         ng-if="layout.adminEdit">
      <fieldset class="gt-fieldset">
        <legend class="gt-legend">
          Test LDAP authentication
        </legend>
        <div class="form-group row">
          <label class="col-xl-3 gt-form-label-xl"
                 for="authTestUsername">
            Username
          </label>
          <div class="col-xl-9">
            <input type="text"
                   class="form-control"
                   ng-model="page.authTestUsername"
                   id="authTestUsername"
                   style="max-width: 15em;">
          </div>
        </div>
        <div class="form-group row">
          <label class="col-xl-3 gt-form-label-xl"
                 for="authTestPassword">
            Password
          </label>
          <div class="col-xl-9">
            <input type="password"
                   class="form-control"
                   ng-model="page.authTestPassword"
                   id="authTestPassword"
                   style="max-width: 15em;">
          </div>
        </div>
        <div class="form-group row">
          <div class="offset-xl-3 col-xl-9">
            <div gt-button
                 gt-btn-class="btn-secondary"
                 gt-label="Test"
                 gt-click="testLdap(deferred)"
                 gt-validate-form="formCtrl">
            </div>
          </div>
        </div>
      </fieldset>
    </div>
  </div>
</div>
<!-- each page with confirmation dialog needs its own confirmation dom so that it is deleted on $destroy -->
<div ng-include="'template/gt-confirmation.html'"></div>
